var express = require('express');
var sign = express.Router();
const signMo = require('../module/sign');
var multer  = require('multer');
sign.post('/add',(req,res)=>{
    let data = req.body;
    signMo.addSign(data,(data,sign_id)=>{
        res.send({status:1,data:'创建签到成功'});
    })
});
sign.get('',(req,res)=>{
    if(req.query){
        let sign_id = req.query.signGuid;
        signMo.getSignList(sign_id, (data)=>{
            res.send({status:1,data:data});
        })
    }else{
        signMo.getSignList(null, data=>{
            res.send({status:1,data:data});
        })
    }
});
//根据创建者学号来查询签到
sign.get('/create',(req,res)=>{
   signMo.getCreaterSigns(req.query.creater,(data)=>{
       res.send({status:1,data});
   })
});
//根据班级号来查询签到以及没有班级号的签到
sign.get('/class',(req,res)=>{
    let class_id = req.query.classGuid;
    signMo.getStudentList(class_id,(err,data)=>{
        if(err){
            res.send({status:0,err:'datatbases err'});
        }else{
            res.send({status:1,data});
        }
    })
});
//学生签到
sign.post('/student',(req,res)=>{
   signMo.studentSign(req.body,(err,data)=>{
       if(err){
           res.send({status:0,err});
       }else {
           res.send({status:1,data:'签到成功'});
       }
   })
});
//删除签到
sign.delete('',(req,res)=>{
   signMo.deleteSignByid(req.query.sign_id,(data)=>{
       if(data.affectedRows==0){
           res.send({status:0,data:'删除签到失败'});
       }else {
           res.send({status:1,data:'删除签到成功'});
       }
   })
});
//修改签到
sign.put('',(req,res)=>{
   signMo.modifySignByid(req.body,(data)=>{
       res.send({status:1,data:'修改签到成功'});
   })
});
//下载签到excel表格
sign.get('/excel',(req,res)=>{
   signMo.exportSignExcel(req.query.sign_id,(excel,title)=>{
       let name = encodeURI(title);
       res.setHeader('Content-Type', 'application/vnd.openxmlformats');
       res.setHeader("Content-Disposition", "attachment; filename=" +name+ ".xlsx");
       res.end(excel, 'binary');
   })
});
module.exports = sign;
